package com.telnetnz.ticketdesk.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;

import com.rockbb.common.utilities.text.JacksonUtils;
import com.telnetnz.ticketdesk.bean.misc.OperationLogObject;
import com.telnetnz.ticketdesk.dao.entity.OperationLogEntity;
import com.telnetnz.ticketdesk.service.OperationLogService;
import com.telnetnz.ticketdesk.service.UserService;

public class OperationLog extends OperationLogEntity
{
	private static final long serialVersionUID = -4229980372920800911L;
	private Map<String, List<String>> parametersMap;

	public Map<String, List<String>> getParametersMap() {
		return parametersMap;
	}
	public void setParametersMap(Map<String, List<String>> parametersMap) {
		this.parametersMap = parametersMap;
	}

	public User getUser()
	{
		return UserService.getUser(getUserId());
	}

	public OperationLogObject getObject()
	{
		return OperationLogService.getObject(getObjectType(), getObjectId());
	}

	public static OperationLog adapt(OperationLogEntity entity)
	{
		if (entity == null)
			return null;
		OperationLog bean = new OperationLog();
		BeanUtils.copyProperties(entity, bean);
		bean.setParametersMap(JacksonUtils.extractMap(entity.getParameters(), new HashMap<String, List<String>>()));
		return bean;
	}

	public OperationLogEntity toEntity()
	{
		if (parametersMap == null) {parametersMap = new HashMap<String, List<String>>();}
		this.setParameters(JacksonUtils.compressMap(parametersMap));
		return this;
	}
}